home *** CD-ROM | disk | FTP | other *** search
- Path: news.channel1.com!channel1!dspse.bedford
- Distribution: world
- Newsgroups: comp.lang.c
- Subject: Dynamic 2-D arrays
- From: dspse.bedford@channel1.com (Dspse Bedford)
- Message-ID: <40.69871.1612@channel1.com>
- Date: Thu, 28 Mar 1996 06:28:00 -0640
- Organization: Channel 1(R) 617-864-0100 Info
-
- I want to define a 2-D array through a pointer-to-pointer structure member
- as shown below:
-
- typedef struct
- { float **matrix;}Obj;
- main()
- {Obj *m;
- int x=5;y=10;
- m=(Obj *)malloc(sizeof(Obj));
- *(m->matrix)=(float *)malloc(x*y*sizeof(float *));
- ...
- ...
- return;}
-
- if I index in the following way: m->matrix[i][j], how does it increment the
- pointer to determine the memory location? If I have
- m->matrix[1][2] and m->matrix[2][1] what is the difference?
-
- In the past, I would use something like whats below, but the difference is
- I do now know ahead of time the Y_dimension or the X_dimension.
-
- #define Y_DIM 10
- #define X_DIM 5
- typedef struct
- {float *matrix[Y_DIM];}Obj;
- main()
- {
- Obj *m;
- m=(Obj *)malloc(sizeof(Obj));
- for (i=0;i<Y_DIM;++i)
- { m->matrix[i]=(float *)malloc(X_DIM*sizeof(float));}
- ..
- return;}
-
- The above ofcourse indexes fine since I create an array of pointers of the
- proper Y-dimension in my structure definition.
-
- Any help would be appreciated.
-
- Thank you,
- Anastasios Maurudis
- anastasios@dspse.com
- DSP Software Engineering, Inc.
- 175 Middlesex Turnpike
- Bedford, MA 01730
- (617)275-3733
- (617)275-4323 Fax
-
-
- ---
- * WR 1.32 # 331 * Blue Wave - what Smurfs do at a football game..
-